/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.mifosplatform.portfolio.self.loanaccount.api;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.portfolio.loanaccount.api.LoanChargesApiResource;
import org.mifosplatform.portfolio.loanaccount.api.LoanTransactionsApiResource;
import org.mifosplatform.portfolio.loanaccount.api.LoansApiResource;
import org.mifosplatform.portfolio.loanaccount.exception.LoanNotFoundException;
import org.mifosplatform.portfolio.self.loanaccount.data.SelfLoansDataValidator;
import org.mifosplatform.portfolio.self.loanaccount.service.AppuserLoansMapperReadService;
import org.mifosplatform.useradministration.domain.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Path("/self/loans")
@Component
@Scope("singleton")
public class SelfLoansApiResource {

	private final PlatformSecurityContext context;
	private final LoansApiResource loansApiResource;
	private final LoanTransactionsApiResource loanTransactionsApiResource;
	private final LoanChargesApiResource loanChargesApiResource;
	private final AppuserLoansMapperReadService appuserLoansMapperReadService;
	private final SelfLoansDataValidator dataValidator;

	@Autowired
	public SelfLoansApiResource(final PlatformSecurityContext context,
			final LoansApiResource loansApiResource,
			final LoanTransactionsApiResource loanTransactionsApiResource,
			final LoanChargesApiResource loanChargesApiResource,
			final AppuserLoansMapperReadService appuserLoansMapperReadService,
			final SelfLoansDataValidator dataValidator) {
		this.context = context;
		this.loansApiResource = loansApiResource;
		this.loanTransactionsApiResource = loanTransactionsApiResource;
		this.loanChargesApiResource = loanChargesApiResource;
		this.appuserLoansMapperReadService = appuserLoansMapperReadService;
		this.dataValidator = dataValidator;
	}

	@GET
	@Path("{loanId}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public String retrieveLoan(@PathParam("loanId") final Long loanId,
			@Context final UriInfo uriInfo) {

		this.dataValidator.validateRetrieveLoan(uriInfo);

		validateAppuserLoanMapping(loanId);

		final boolean staffInSelectedOfficeOnly = false;
		return this.loansApiResource.retrieveLoan(loanId,
				staffInSelectedOfficeOnly, uriInfo);
	}

	@GET
	@Path("{loanId}/transactions/{transactionId}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public String retrieveTransaction(@PathParam("loanId") final Long loanId,
			@PathParam("transactionId") final Long transactionId,
			@Context final UriInfo uriInfo) {

		this.dataValidator.validateRetrieveTransaction(uriInfo);

		validateAppuserLoanMapping(loanId);

		return this.loanTransactionsApiResource.retrieveTransaction(loanId,
				transactionId, uriInfo);
	}

	@GET
	@Path("{loanId}/charges")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public String retrieveAllLoanCharges(
			@PathParam("loanId") final Long loanId,
			@Context final UriInfo uriInfo) {

		validateAppuserLoanMapping(loanId);

		return this.loanChargesApiResource.retrieveAllLoanCharges(loanId,
				uriInfo);
	}

	@GET
	@Path("{loanId}/charges/{chargeId}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public String retrieveLoanCharge(@PathParam("loanId") final Long loanId,
			@PathParam("chargeId") final Long loanChargeId,
			@Context final UriInfo uriInfo) {

		validateAppuserLoanMapping(loanId);

		return this.retrieveLoanCharge(loanId, loanChargeId, uriInfo);
	}

	private void validateAppuserLoanMapping(final Long loanId) {
		AppUser user = this.context.authenticatedUser();
		final boolean isLoanMappedToUser = this.appuserLoansMapperReadService
				.isLoanMappedToUser(loanId, user.getId());
		if (!isLoanMappedToUser) {
			throw new LoanNotFoundException(loanId);
		}
	}

}